<?php
namespace app\admin\controller;
use think\Loader;
vendor('excel.PHPExcel');
class Excel
{
	
	//导出excel表
	//$data 二维数组
	//$name 导出的表格命名
	public function export($data,$name){
		Loader::import('excel.PHPExcel');//引入PHPExcel.php
		$PHPExcel = new \PHPExcel();//实例化
		$PHPExcel->getActiveSheet()->setTitle('Backup'); //给当前活动sheet设置名称
		$PHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
		$PHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$PHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		foreach($data as $k => $v){
			$num=$k+2;
			//定义标题
			$PHPExcel->setActiveSheetIndex(0)
				->setCellValue('A1', 'ID')
				->setCellValue('B1', '用户名')
				->setCellValue('C1', '手机')
				->setCellValue('D1', 'QQ')
				->setCellValue('E1', '姓名');
			$PHPExcel->setActiveSheetIndex(0)
			//Excel的第A列，id是你查出数组的键值，下面以此类推
			->setCellValue('A'.$num, $v['id']) 
			->setCellValue('B'.$num, $v['username'])
			->setCellValue('C'.$num, $v['mobile'])
			->setCellValue('D'.$num, $v['qq'])
			->setCellValue('E'.$num, $v['bankren']);
		}
		$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//创建生成的格式
		
		header('Content-Disposition: attachment;filename='.$name.'.xlsx');//下载下来的表格名
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		$PHPWriter->save("php://output");//输出文件
		exit;
	}
}